
export class OverTimeRecorder {
    private _breakTime: number = null;
    private _curTime: number = null;

    constructor(breakTime: number) {
        this._breakTime = breakTime;
        this._curTime = null;
    }

    mark(): boolean {
        let time = TimeUtils.getTimeSec();

        if (null == this._curTime) {
            this._curTime = time;
            return false;
        }

        let time_ = time - this._curTime;
        this._curTime = time;
        return time_ > this._breakTime ;
    }

    clear() {
        this._curTime = null;
    }
}


export class TimeUtils {

    static getTime(): number {
        return new Date().getTime();
    }

    static getTimeSec(): number {
        return this.getTime() / 1000;
    }

    static getTimeMill(): number {
        return this.getTime();
    }

    // static getTotalTimeSec(): number {
    //     return cc.director.getTotalTime() / 1000;
    // }

    // static getTotalTimeMill(): number {
    //     return cc.director.getTotalTime();
    // }

    static isPassTime(time: number, passTime): boolean {
        return this.getTime() - time > passTime * 1000;
    }

    static isSameDay(time1: number, time2: number): boolean {
        return new Date(time1).toDateString() == new Date(time2).toDateString();
    }

    static getDiffStdDay(timeSec1: number, timeSec2: number): number {
        return Math.floor(timeSec2 / 86400) - Math.floor(timeSec1 / 86400);
    }

    static formatDate(date, fmt = "yyyy-MM-dd hh:mm:ss") {
        let o = {
            "M+": date.getMonth() + 1, // 月份
            "d+": date.getDate(), // 日
            "h+": date.getHours(), // 小时
            "m+": date.getMinutes(), // 分
            "s+": date.getSeconds(), // 秒
            "q+": Math.floor((date.getMonth() + 3) / 3), // 季度
            "S": date.getMilliseconds() // 毫秒
        };
        if (/(y+)/.test(fmt))
            fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (let k in o)
            if (new RegExp("(" + k + ")").test(fmt))
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        return fmt;
    }

}
